package tt.dz.egobus.servicemanager.service.impl;

import org.springframework.stereotype.Service;
import tt.dz.egobus.api.ResponseBack;
import tt.dz.egobus.api.usercenter.admin.AdminBack;
import tt.dz.egobus.servicemanager.base.service.AbstractFacadeServiceConfig;
import tt.dz.egobus.servicemanager.core.exception.BusinessException;
import tt.dz.egobus.servicemanager.facade.interator.FacadeFactory;
import tt.dz.egobus.servicemanager.facade.interator.FacadeFactoryLoggerInterceptorHolder;
import tt.dz.egobus.servicemanager.facade.user.AdminApiServiceFacade;
import tt.dz.egobus.servicemanager.service.WebCommonService;
import tt.dz.egobus.servicemanager.vo.SystemUserVo;

import javax.annotation.PostConstruct;

/**
 * @author Anson
 * @date 17-3-24
 * @name 公共服务层
 * @since 1.0.2
 */
@Service("webCommonServiceImpl")
public class WebCommonServiceImpl extends AbstractFacadeServiceConfig implements WebCommonService {

    @Override
    @PostConstruct
    public FacadeFactory getFacadeFactory() {
        return FacadeFactoryLoggerInterceptorHolder.LOGGER_FACADE;
    }


    @Override
    public SystemUserVo login(String userName, String password, String ip) throws BusinessException {
        AdminApiServiceFacade facade = getAdminFacade();
        ResponseBack<AdminBack> responseBack = facade.loginManager(userName, password, ip);
        if (!responseBack.isSuccess() || responseBack.getResult() == null) {
            return null;
        }
        AdminBack back = responseBack.getResult();
        SystemUserVo vo = new SystemUserVo();
        vo.setId(back.getId());
        vo.setUsername(back.getUsername());
        return vo;
    }

    @Override
    public Boolean logout(String userName) throws BusinessException {

        return true;
    }

    @Override
    public boolean changepasswd(String userId, String userName, String password, String passwordOld) {
        AdminApiServiceFacade facade = getAdminFacade();
        ResponseBack<AdminBack> adminBackResponseBack = facade.modificationAdmin(userId, password, passwordOld);
        return adminBackResponseBack.isSuccess() && adminBackResponseBack.getResult() != null;
    }

    private AdminApiServiceFacade getAdminFacade() {
        return this.facade("admin", AdminApiServiceFacade.class);
    }


}
